<?php

namespace App\Http\Controllers\Web\Seller;

use App\ConstDir\ErrorConst;
use App\Http\Controllers\Web\BaseController;
use Illuminate\Http\Request;
use App\Repository\Repositories\Interfaces\OrderContractRepository;

 
class OrderContractController extends BaseController
{
   
    protected $order_contract_repo;
    protected $user_id;
    
    public function __construct(OrderContractRepository $order_contract_repo_repo)
    {
        parent::__construct();
        $this->order_contract_repo = $order_contract_repo_repo;
  
        $this->user_id =isset($this->user) &&  isset($this->user->id) ? $this->user->id : 0;
        
        $this->order_contract_repo->scopeQuery(function ($query) {
            return $query->where(['seller_user_id'=>$this->user_id]);
        });
    }

    public function index()
    {
        $orders = $this->order_contract_repo->paginate($this->perPage);
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $orders, true);
    }

    public function show($id)
    {
        $product = $this->order_contract_repo->find($id);
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $product['data']);
    }
    
    public function default($id)
    {
        $product = $this->order_contract_repo->find($id);
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $product['data']);
    }

    public function store(Request $request)
    { 
        $this->validate($request,[
            'order_id'=> 'required|integer',
        ]);
        
        $data = $request->all();
 
        $ret = $this->order_contract_repo->create($data);
        
        return $this->noDataReturn($ret ? true : false);
    }

    public function update(Request $request, $id)
    { 
        $this->validate($request,[
            'order_id'=> 'integer',
            'file_url'=>'string',
        ]);
        
        $data = $request->all();
        
        $ret = $this->order_contract_repo->update($data, $id);
        
        return $this->noDataReturn($ret ? true : false);
    }

    public function delete($id)
    {
        $ret = $this->order_contract_repo->delete($id);
        return $this->noDataReturn($ret ? true : false);
    }
    
    public function findByOrderId($order_id){
        $result = $this->order_contract_repo->findWhere(['order_id'=>$order_id]);
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $result['data']);
    }
    
  
}
